/* Component info -------------------------------------------------------------
 * Author   : PURESUN
 * Version  : V1.0.0
 * Notes    : main header file
 * Update   : 2023-12-17
 -----------------------------------------------------------------------------*/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H__
#define __MAIN_H__

/* Includes ------------------------------------------------------------------*/
#include "pub_types.h"
#include "ui.h"
#include "coating_pro.h"
#include "serial.h"
#include "vaacum.h"
#include "syscfg.h"
// #include "log.h"
#include "esp32.h"
#include "485_slave.h"
#include "puresun_func.h"

/* Exported macro ------------------------------------------------------------*/
#define DECIVE_CMD_RING_SIZE 10
#define DECIVE_CMD_RING_LEN 32
#define PORT_OPEN 0x96
#define PORT_CLOSE 0x69

#define VACCUM_ADDR 0x07
#define MOLEPUMP_ADDR 0x10
#define ESP32_ADDR 0x50
/* Exported typedef ----------------------------------------------------------*/

// format : rfu lenth adrh adrl
//           00   02   00   6B
// clock_t start_time;
time_t start_time;
typedef enum
{
    MPUMP_FIRST_ADR = 0x0010,
    VACUUM_FIRST_ADR = 0x0020,
    JPUMP_FIRST_ADE = 0x0060
} screen_reg_t;
typedef enum
{
    MPUMP_REG_ADR_CTL = 0x00012000,
    MPUMP_REG_ADR_STATUS = 0x00041000,
    MPUMP_REG_ADR_TEMP = 0x00011004, // temperature
} mpump_reg_map_t;

typedef enum
{
    ESP32_REG_ADR_DEVICE_STATUS = 0x0010,
    ESP32_REG_ADR_VACUUM = 0x00A1,
} vaccum_reg_map_t;


typedef struct _relay_ctl_t
{
    byte cmd[DECIVE_CMD_RING_LEN];
    int len;
    bool upated;
} relay_ctl_t;

typedef enum
{
    PAGE_START = 0x00,
    PAGE_MAIN = 0x01,
    PAGE_PROGRAM = 0x02,
    PAGE_DEVICE = 0x03,
    PAGE_VACUUM = 0x04,
    PAGE_SETTING = 0x05,
} current_page_t;

typedef struct
{
    int16_t x;
    int16_t y;
    int16_t offset_x;
    int16_t offset_y;
    float transparence;
    float angle;
} img_map_t;

typedef struct __adjuster_t
{
    uint16_t register_value[25];
    bool flag_set_power_time;
} adjuster_t;

typedef struct _device_t
{
    coating_program_list_t cpl;
    ui_t ui;
    serial_t rs485;
    bool mpump_status;
    bool heater_status;
    bool valve_main_status;
    bool valve_up_status;
    bool valve_down_status;
    bool valve_release_status;
    bool valve_back_status;
    bool jpump_status;
    bool start;
    relay_ctl_t relay_ctl;
    vaacum_t vaacum;
    adjuster_t adjuster;
    current_page_t current_page;
    syscfg_t syscfg;
    struct
    {
        pthread_t thread;
        bool start;
        bool change_piece;
        bool change_piece_ready;
    } autorun;
    struct
    {
        bool update;
        string content;
    } log;
    // log_usr log_usr;
    esp32_t esp32;
    uint16_t device_status;
    slave485_t slave485;
} device_t;


extern byte cmd_set_port[];

/* Exported variables --------------------------------------------------------*/
extern device_t device;

/* Exported function prototypes ----------------------------------------------*/
void sync_relay(void);

#endif /* __MAIN_H__ */